<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/17
 * Time: 15:21
 */

namespace app\meeting\controller;


use app\meeting\model\MeetCategoryModel;
use cmf\controller\AdminBaseController;

class AdminCategoryController extends AdminBaseController
{
    public function index()
    {

        $categories = MeetCategoryModel::all();
        $this->assign('categories', $categories);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit($id)
    {
        $category = MeetCategoryModel::get($id);
        $this->assign('category', $category);
        return $this->fetch();
    }

    public function delete($id){
        if($id){
           $result = MeetCategoryModel::destroy($id);
            if($result){
                $this->success('删除成功');
            }else {
                $this->error('删除失败');
            }
        }
    }

    public function addPost()
    {
        $name = $this->request->param('name');
        if (empty($name)) {
            $this->error('名称不能为空');
        }
        $categoryModel = new MeetCategoryModel();
        $result = $categoryModel->allowField(true)->save($this->request->param());
        if ($result) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    public function editPost($id)
    {
        $name = $this->request->param('name');
        if (empty($name)) {
            $this->error('名称不能为空');
        }
        $categoryModel = MeetCategoryModel::get($id);
        $categoryModel->name = $name;
        $result = $categoryModel->save();
        if ($result) {
            $this->success('编辑成功');
        } else {
            $this->error('编辑失败');
        }
    }
}